package cn.com.jcoo;

import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.regex.Pattern;

public class StringUtils extends org.apache.commons.lang3.StringUtils {
    /**
     * 下划线
     */
    private static final char SEPARATOR = '_';

    public static boolean hasLength(String s1) {
        return org.springframework.util.StringUtils.hasLength(s1);
    }

    public static String ifEmpty(String value, String k1, String k2) {
        return StringUtils.isEmpty(value) ? k1 : k2;
    }

    public static <T> String defaultIfEmpty(T data, Function<T, String> getFunction,
                                            String defaultValue) {
        if (data == null) {
            return defaultValue;
        }
        return defaultIfEmpty(getFunction.apply(data), defaultValue);
    }

    public static String defaultIfEmpty(String str1, String defaultStr1,
                                        String defaultStr2) {
        return defaultIfEmpty(defaultIfEmpty(str1, defaultStr1), defaultStr2);
    }

    /**
     * 查找指定字符串是否匹配指定字符串列表中的任意一个字符串
     *
     * @param str    指定字符串
     * @param values 需要检查的字符串数组
     * @return 是否匹配
     */
    public static boolean matchAny(String str, List<String> values) {
        if (isEmpty(str) || CollectionUtils.isEmpty(values)) {
            return false;
        }
        for (String pattern : values) {
            if (isMatch(pattern, str)) {
                return true;
            }
        }
        return false;
    }

    public static boolean isMatch(String regex, CharSequence content) {
        if (content == null) {
            return false;
        } else if (StringUtils.isEmpty(regex)) {
            return true;
        } else {
            Pattern pattern = Pattern.compile(regex, Pattern.DOTALL);
            return isMatch(pattern, content);
        }
    }

    public static boolean isMatch(Pattern pattern, CharSequence content) {
        return content != null && pattern != null ? pattern.matcher(content).matches() : false;
    }

    /**
     * 驼峰转下划线命名
     */
    public static String toUnderScoreCase(String str) {
        if (str == null) {
            return null;
        }
        StringBuilder sb = new StringBuilder();
        // 前置字符是否大写
        boolean preCharIsUpperCase;
        // 当前字符是否大写
        boolean currentCharIsUpperCase;
        // 下一字符是否大写
        boolean nextCharIsUpperCase = true;
        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            if (i > 0) {
                preCharIsUpperCase = Character.isUpperCase(str.charAt(i - 1));
            } else {
                preCharIsUpperCase = false;
            }

            currentCharIsUpperCase = Character.isUpperCase(c);

            if (i < (str.length() - 1)) {
                nextCharIsUpperCase = Character.isUpperCase(str.charAt(i + 1));
            }

            if (preCharIsUpperCase && currentCharIsUpperCase && !nextCharIsUpperCase) {
                sb.append(Character.START_PUNCTUATION);
            } else if (i != 0 && !preCharIsUpperCase && currentCharIsUpperCase) {
                sb.append(SEPARATOR);
            }
            sb.append(Character.toLowerCase(c));
        }

        return sb.toString();
    }

    /**
     * 包含任意字符串忽略大小写
     *
     * @param str    字符串
     * @param values 匹配串
     */
    public static boolean containsAnyIgnoreCase(String str, String... values) {
        if (str != null && values != null) {
            for (String s : values) {
                if (str.equalsIgnoreCase(trim(s))) {
                    return true;
                }
            }
        }
        return false;
    }

    public static boolean equalsAny(String str, List<String> values) {
        if (isEmpty(str) || CollectionUtils.isEmpty(values)) {
            return false;
        }
        String s = values.stream().filter(item -> StringUtils.equals(str, item))
                .findFirst().orElse(null);
        return s != null;

    }

    public static boolean isNull(Object searchValue) {
        if (null == searchValue) {
            return true;
        } else if (searchValue instanceof String) {
            return isEmpty((String) searchValue);
        }
        return false;
    }

    public static boolean equalsIgnoreEmpty(Object o1, Object o2) {
        return StringUtils.equals(StringUtils.ifEmpty((String) o1, null, (String) o1),
                StringUtils.ifEmpty((String) o2, null, (String) o2));
    }

    public static void main(String[] args) {
        ArrayList<String> strings = new ArrayList<>(30);
        for (int i = 0; i < 5; i++) {
            strings.add(String.valueOf(i + 100));
        }
        Map<Integer, String> map = new HashMap<>(4);
        List<Integer> list = new ArrayList<>();
        while (map.size() < 4) {
            int i1 = (int) (Math.random() * 5);
            map.put(i1, strings.get(i1));
            list.add(i1);
        }
        System.out.println(list);
    }
}
